<script setup>
    import {ref, reactive, defineEmits} from "vue";
    import {ElMessage} from "element-plus";
    import axios from "axios";
    let form = reactive({
      reviewId:"",
      courseId:"",
      userId:"",
      content:"",
      createTime:"",
      rating:""
    });

    // 添加自定义事件
    const emite = defineEmits(["addValue"]);

    async function saveAction() {
      try {
        console.log("-----------------------");
        console.log(form);

        // 发送 POST 请求到后端
        const response = await axios.post('/api/courseReview/add', form);

        console.log('保存成功:', response.data);

        // 显示成功消息
        ElMessage.success('课程评价添加成功');

        // 清空表单
        form = reactive({
          reviewId:"",
          courseId:"",
          userId:"",
          content:"",
          createTime:"",
          rating:""
        });
      } catch (error) {
        console.error('保存失败:', error);

        // 显示错误消息
        ElMessage.error('课程评价添加失败');
      }
      emite("addValue", form);
    }
</script>

<template>
    <el-form :model="form">

        <el-form-item label="评价课程：" label-width="100">
            <el-input v-model="form.courseId" :placeholder="请输入课程名称"></el-input>
        </el-form-item>
      <el-form-item label="评价人：" label-width="100">
        <el-input v-model="form.userId" :placeholder="请"></el-input>
      </el-form-item>
      <el-form-item label="评价内容：" label-width="100">
        <el-input v-model="form.content" :placeholder="请"></el-input>
      </el-form-item>
      <el-form-item label="评价时间：" label-width="100">
        <el-input v-model="form.createTime" :placeholder="请"></el-input>
      </el-form-item>
      <el-form-item label="评价星级：" label-width="100">
        <el-input v-model="form.rating" :placeholder="请"></el-input>
      </el-form-item>

    </el-form>
    <div style="display: flex; justify-content: center;">
      <el-button @click="saveAction">保存</el-button>
    </div>
</template>

<style scoped lang="less">

</style>